/**
 * 登陆
 */
function login() {
    // 获取表单对象
    var bootstrapValidator = $('#login-form').data('bootstrapValidator');
    // 验证表单
    bootstrapValidator.validate();
    // 是否通过了验证
    if (!$('#login-form').data('bootstrapValidator').isValid()) {
	return;
    }
    var data = $('#login-form').serializeJSON();
    console.log(data);
    $.ajax({
	type : "POST",
	dataType : "json",
	contentType : "application/json;charset=utf-8",
	url : "/login",
	data : JSON.stringify(data),// 这里要传json字符串
	success : function(result) {
	    if (result.status == 200) {
		var resultdata = result.data;
		if (resultdata) {
		    // 保存临时的cookie
		    setCookie("username", resultdata.username);
		    setCookie("rolename", resultdata.rolename);
		    // 如果点击了记住我，那么存入localstorage
		    rememberMe($("input[name='rememberMe']").is(":checked"));
		}
		// 跳转主页
		window.location.href = "/";
	    } else {
		new LoginValidator({
		    code : result.status,
		    message : result.msg,
		    username : $("input[name='username']").val(),
		    password : $("input[name='password']").val()
		});
	    }
	},
	error : function() {
	    modals.info("登陆异常");
	}
    });

}

function register() {
    // 是否同意了条款
    if (!$("#agreement").is(":checked")) {
	modals.info("请先勾选同意遵循IMSysyem协议");
	return;
    }
    // 获取表单对象
    var bootstrapValidator = $('#register-form').data('bootstrapValidator');
    // 验证表单
    bootstrapValidator.validate();
    // 是否通过了验证
    if (!$('#register-form').data('bootstrapValidator').isValid()) {
	return;
    }
    // 获取表单内容
    var data = $('#register-form').serializeJSON();
    console.log(data);
    $.ajax({
	type : "POST",
	dataType : "json",
	contentType : "application/json;charset=utf-8",
	url : "/register",
	data : JSON.stringify(data),// 这里要传json字符串
	success : function(result) {
	    var resultdata = result.data;
	    if (resultdata) {
		if(result.status == 500){
		    modals.info(result.msg);
		    return ;
			}
	    }
	    setCookie("username", resultdata.username);
		setCookie("rolename", resultdata.rolename);
	    // 跳转主页
	    window.location.href = "/";
	},
	error : function() {
	    modals.info("注册异常");
	}
    });
}

function LoginValidator(config) {
    this.code = config.code;
    this.message = config.message;
    this.userName = config.username;
    this.password = config.password;
    this.initValidator();
}

// 0 未授权 1 账号问题 2 密码错误 3 账号密码错误 
LoginValidator.prototype.initValidator = function() {
    if (!this.code)
	return;
    if (this.code == 0) {
	this.addPasswordErrorMsg();
    } else if (this.code == 1) {
	this.addUserNameErrorStyle();
	this.addUserNameErrorMsg();
    } else if (this.code == 2) {
	this.addPasswordErrorStyle();
	this.addPasswordErrorMsg();
    } else if (this.code == 3) {
	this.addUserNameErrorStyle();
	this.addPasswordErrorStyle();
	this.addPasswordErrorMsg();
    }
    return;
}

LoginValidator.prototype.addUserNameErrorStyle = function() {
    this.addErrorStyle('username');
}

LoginValidator.prototype.addPasswordErrorStyle = function() {
    this.addErrorStyle('password');
}

LoginValidator.prototype.addUserNameErrorMsg = function() {
    this.addErrorMsg('username');
}

LoginValidator.prototype.addPasswordErrorMsg = function() {
    this.addErrorMsg('password');
}

LoginValidator.prototype.addErrorMsg = function(field) {
    // 清除掉之前的提示内容
    $("input[name='" + field + "']").parent().children('small').remove();
    // 更新错误提示信息
    $("input[name='" + field + "']").parent().append(
	    '<small  data-bv-validator="notEmpty" data-bv-validator-for="'
		    + field + '" class="help-block">' + this.message
		    + '</small>');
}

LoginValidator.prototype.addErrorStyle = function(field) {
    $("input[name='" + field + "']").parent().addClass("has-error");
}

// 使用本地缓存记住用户名密码
function rememberMe(rm_flag) {
    // remember me
    if (rm_flag) {
	localStorage.username = $("input[name='username']").val();
	localStorage.password = $("input[name='password']").val();
	localStorage.rememberMe = 1;
    }
    // delete remember msg
    else {
	localStorage.userName = null;
	localStorage.password = null;
	localStorage.rememberMe = 0;
    }
}
// 记住回填
function fillbackLoginForm() {
    if (localStorage.rememberMe && localStorage.rememberMe == "1") {
	$("input[name='username']").val(localStorage.username);
	$("input[name='password']").val(localStorage.password);
	$("input[name='rememberMe']").iCheck('check');
	$("input[name='rememberMe']").iCheck('update');
    }
}